package cn.com.huzo.webapp.controller.master;

import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.model.entities.master.InspectionStandardEntity;
import cn.com.huzo.service.master.InspectionStandardManager;
import cn.com.huzo.service.master.PoTypeManager;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 化检标准信息控制器
 *
 * @author 吴旭
 */
@Controller
public class InspectionStandardController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">    
    @Resource(name = "InspectionStandardManager")
    private InspectionStandardManager _manager;

    @Resource(name = "PoTypeManager")
    private PoTypeManager _manager_po_type;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到化检标准一览画面
     * <p>
     * @return
     */
    @RequestMapping("master/inpstandard/list.html")
    public ModelAndView redirectListWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("master.inpstandard.list.def");
        return view;
    }

    /**
     * 品质要求编辑画面
     * <p>
     * @param session
     * @param action 操作区分
     * @param standardId 品质要求标识
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/inpstandard/{action}/{standardId}/edit.html",
            method = {RequestMethod.GET})
    public ModelAndView redirectEditWnd(HttpSession session, @PathVariable(value = "action") int action,
            @PathVariable(value = "standardId") Long standardId) throws Exception {
        ModelAndView view = new ModelAndView();

        view.addObject("PoTypeList", _manager_po_type.fetchAll());  // 设置采购订单类型
        view.addObject("action", action);
        view.addObject("standardId", standardId);

        view.setViewName("master.inpstandard.edit.def");
        return view;
    }

    /**
     * 弹出化检标准选择画面
     * <p>
     * @return
     */
    @RequestMapping("master/inpstandard/choice.html")
    public ModelAndView redirectInspectionStandardPopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.master.inpstandard.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 根据标识取得化检标准数据。
     *
     * @param criterion 化检标准标识、当前用户、流程标识
     * @return 化检标准数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/inpstandard/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    InspectionStandardEntity fetchByKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.fetchByKey(MapUtils.getLong(criterion, "HeaderId"));
    }

    /**
     * 删除。
     *
     * @param criterion 要删除的数据
     * @return 返回删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/inpstandard/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.delete(criterion);
    }

    /**
     * 根据条件取得化检标准信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 化检标准信息分页数据
     * @throws Exception
     */
    @RequestMapping(value = "master/inpstandard/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.fetchForPage(criterion);
    }

    /**
     * 保存化检标准信息数据。
     * <p>
     * @param instance 要保存的化检标准信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "master/inpstandard/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody InspectionStandardEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getStandardId() > 0) { // 编辑
            rowsAffected = (long) _manager.update(instance);
        } else {                            // 新增
            rowsAffected = _manager.insert(instance);
        }

        return rowsAffected;
    }
    // </editor-fold>
}
